package org.mics.core.config;

import org.springdoc.core.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;

@Configuration
public class SpringDocSwaggerConfiguration{
	public static final String SWAGGER_SCAN_BASE_PACKAGE = "org.mics";
	public static final String VERSION = "1.0.0";
	private static final String headerName = "Authorization";//请求头名称
	
	@Bean
    public GroupedOpenApi publicApi() {
        return GroupedOpenApi.builder() 
                .group("public")
                .packagesToScan(SWAGGER_SCAN_BASE_PACKAGE)
                .addOperationCustomizer((operation, handlerMethod) -> {
                    operation.addSecurityItem(new SecurityRequirement().addList(headerName));
                    return operation;
                })
                .build();
    }
	
	@Bean
    public OpenAPI openAPI() {
        Components components = new Components();
        //添加右上角的统一安全认证
        components.addSecuritySchemes(headerName,
                new SecurityScheme()
                        .type(SecurityScheme.Type.APIKEY)
                        .scheme("basic")
                        .name(headerName)
                        .in(SecurityScheme.In.HEADER)
                        .description("请求头")
        );
        return new OpenAPI()
                .components(components)
                .info(apiInfo());
    }

	private Info apiInfo() {
		Contact contact = new Contact();
		contact.setEmail("490359559@qq.com");
		contact.setName("flym");
       return new Info()
                   .title("接口文档") //设置文档的标题
                   .contact(contact)
                   .description("接口文档") // 设置文档的描述
                   .version(VERSION); // 设置文档的信息-> 1.0.0 Version information
	}
}

